/**
 * project : sirius_tiger
 * user created : pippo
 * date created : 2007-8-10-下午12:09:29
 */
package com.sihai.core.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 用ThreadLocal提供一个存储线程内变量的地方.
 * <p>
 * 客户端代码可以用静态方法存储和获取线程内变量,不需要依赖于HttpSession. web层的Controller可通过此处向business层传入user_id之类的变量
 */
@SuppressWarnings("unchecked")
public final class UserContext {

	public final static String current_user = "com.sihai.ehr.current.user";

	public final static String current_operator_id = "current.operator.id";

	private static final ThreadLocal<Map<String, Object>> context = new ThreadLocal<Map<String, Object>>() {

		@Override
		protected Map<String, Object> initialValue() {
			return new HashMap<String, Object>(5);
		}
	};

	public static Map<String, Object> get() {
		return context.get();
	}

	public static void set(Map<String, Object> values) {
		context.set(values);
	}

	public static <T> T get(String attribute) {
		Map<String, Object> map = context.get();
		return (T) (map != null ? map.get(attribute) : null);
	}

	public static void set(String attribute, Object value) {
		context.get().put(attribute, value);
	}

	public static <T> T getOperatorId() {
		return (T) get(current_operator_id);
	}

}
